package ThreadPool;

import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class MyThreadPoolExecutor {
    ExecutorService threadPool = new ThreadPoolExecutor(
            3, 5, 1L, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(3), Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.AbortPolicy());

    @Test
    void test() {
        //System.out.println(new BigDecimal("111.333E-12").toPlainString());

        // System.out.println(Runtime.getRuntime().availableProcessors());   //查看CPU核数
        //线程池7大参数
        ExecutorService threadPool = new ThreadPoolExecutor(
                3, 5, 1L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3), Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());

        List<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>();
        Callable<Integer> task = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                System.out.println(1);
                return 1;
            }
        };
        tasks.add(task);
        Callable<Integer> task1 = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                System.out.println(2);
                return 1;
            }
        };
        tasks.add(task1);

        try {
            threadPool.invokeAll(tasks);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("33");
    }

    public static void main(String[] args) {
        long begint = System.currentTimeMillis();
        for (int i = 0; i < 170000; i++) {
            new Thread(() -> {
                System.out.println(1111);
            }).start();

        }
        long end = System.currentTimeMillis();
        System.out.println((end - begint) / 1000);
    }


    @Test
    void test1() {
        System.out.println(this.test11());
        System.out.println(this.test12());
        System.out.println(this.test11());


        System.out.println("33");
    }

    public int test11() {
        Future<Integer> a = this.threadPool.submit(() -> {
            System.out.println(1);
            return 11;
        });
        try {
            return a.get();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public int test12() {
        Future<Integer> a = this.threadPool.submit(() -> {
            System.out.println(2);
            return 22;
        });
        try {
            return a.get();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
